import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/bean/lot_entity.dart';
import 'package:xin_nuo_pda/values/colors.dart';
import 'package:xin_nuo_pda/values/constant.dart';
import 'package:xin_nuo_pda/views/SubTitleBar.dart';
import 'package:xin_nuo_pda/views/TextView.dart';
import 'package:xin_nuo_pda/views/edittext/ClearEditText.dart';
import 'package:xin_nuo_pda/views/edittext/TitleEditText.dart';
import 'package:xin_nuo_pda/utils/string_extension.dart';

import 'QcTrackOutController.dart';

class QcTrackOutPage extends StatelessWidget {
  final controller = Get.put(QcTrackOutController());
  final state = Get
      .find<QcTrackOutController>()
      .state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(USER),
      ),
      body: SingleChildScrollView(
        child: GetBuilder<QcTrackOutController>(
          builder: (controller) =>
              Container(
                child: Column(
                  children: [
                    SubTitleBar("结束作业", "提交", () {
                      controller.submit();
                    }),
                    TitleEditText("批次ID：", "请扫描批次ID", (valuer) {
                      controller.getLotInfo(valuer);
                    }, controller: state.lotController,),
                    GetBuilder<QcTrackOutController>(builder: (controller) {
                      return TextView(
                          '批次数量', state.lotEntity?.mAINQTY.toString() ?? '0');
                    },id: state.lotEntityID,),
                    TitleEditText("抽检数量：", "请输入抽检数量", (valuer) {

                    }, controller: state.qtyController, isScan: false,),
                    Container(
                      height: 48,
                      padding: const EdgeInsets.all(8),
                      decoration: BoxDecoration(
                        color: Colors.white,
                      ),
                      width: double.infinity,
                      child: Stack(
                        children: [
                          Align(
                            alignment: Alignment.centerLeft,
                            child: Text(
                              "缺陷信息",
                              style: TextStyle(
                                  fontSize: 16, fontWeight: FontWeight.bold),
                            ),
                          ),
                          // Align(
                          //   alignment: Alignment.centerRight,
                          //   child: Container(
                          //     height: 30,
                          //     margin: EdgeInsets.only(right: 8),
                          //     child: OutlinedButton(
                          //       onPressed: () {
                          //           Fluttertoast.showToast(msg: "点击了保存按钮");
                          //       },
                          //       child: Text("保存"),
                          //     ),
                          //   ),
                          // )
                        ],
                      ),
                    ),
                    // GetBuilder<QcTrackOutController>(
                    //   builder: (controller) => Align(
                    //     alignment: Alignment.center,
                    //     child: Row(
                    //       children: [
                    //         SizedBox(
                    //           width: 12,
                    //         ),
                    //         Text(
                    //           "不良代码",
                    //           style: TextStyle(
                    //             color: text_black.toColor(),
                    //           ),
                    //         ),
                    //         SizedBox(
                    //           width: 8,
                    //         ),
                    //         Expanded(
                    //             flex: 1,
                    //             child: Container(
                    //               padding: EdgeInsets.only(left: 4),
                    //               height: 36,
                    //               decoration: BoxDecoration(
                    //                   border: new Border.all(
                    //                       width: 1.0, color: theme_blue.toColor()),
                    //                   borderRadius:
                    //                   BorderRadius.all(Radius.circular(4))),
                    //               child: DropdownButtonHideUnderline(
                    //                 child: DropdownButton(
                    //                   value: state.selectDefectValue,
                    //                   items: getList(),
                    //                   onChanged: (value) {
                    //                     controller.updateSelectDefectValue(
                    //                         value as LotSCRAPCODELIST);
                    //                   },
                    //                   hint: Text(
                    //                     "请选择不良代码",
                    //                     style: TextStyle(
                    //                       fontSize: 14,
                    //                     ),
                    //                   ),
                    //                 ),
                    //               ),
                    //             )),
                    //         SizedBox(
                    //           width: 8,
                    //         ),
                    //         Container(
                    //           height: 36,
                    //           margin: EdgeInsets.only(right: 8),
                    //           child: ElevatedButton(
                    //             onPressed: () {
                    //               controller.addDefectData();
                    //             },
                    //             child: Text("保存"),
                    //           ),
                    //         ),
                    //         SizedBox(
                    //           width: 4,
                    //         ),
                    //       ],
                    //     ),
                    //   ),
                    //   id: state.defectCodeLogicId,
                    // ),
                    SizedBox(
                      height: 4,
                    ),
                    GetBuilder<QcTrackOutController>(
                        builder: (controller) =>
                            Table(
                              defaultVerticalAlignment:
                              TableCellVerticalAlignment.middle,
                              columnWidths: {
                                //列宽
                                0: FlexColumnWidth(2.0),
                                1: FlexColumnWidth(2.0),
                                2: FlexColumnWidth(1.0),
                              },
                              border: TableBorder.all(
                                color: Colors.grey,
                                width: 1.0,
                                style: BorderStyle.solid,
                              ),
                              children: getDefectList(context),
                            ),
                        id: state.defectTableLogicId),
                    // GetBuilder<QcTrackOutController>(
                    //     builder: (controller) =>
                    //         TextView("不良数量", state.defectCount.toString()),
                    //     id: state.defectCountLogicId),
                    // GetBuilder<QcTrackOutController>(
                    //   builder: (controller) => Table(
                    //     defaultVerticalAlignment: TableCellVerticalAlignment.middle,
                    //     columnWidths: {
                    //       //列宽
                    //       0: FlexColumnWidth(1.0),
                    //       1: FlexColumnWidth(1.0),
                    //       2: FlexColumnWidth(1.0),
                    //       3: FlexColumnWidth(1.0),
                    //       4: FlexColumnWidth(1.0),
                    //       5: FlexColumnWidth(1.0)
                    //     },
                    //     border: TableBorder.all(
                    //       color: Colors.grey,
                    //       width: 1.0,
                    //       style: BorderStyle.solid,
                    //     ),
                    //     children: getCheckList(context),
                    //   ),
                    //   // id: state.tableControllerId,
                    // ),
                  ],
                ),
              ),
        ),
      ),
    );
  }

  List<DropdownMenuItem<LotSCRAPCODELIST>> getList() {
    List<DropdownMenuItem<LotSCRAPCODELIST>> mList = [];
    state.scrapCodeList.forEach((element) {
      mList.add(DropdownMenuItem(
        child: Text(element.dESCRIPTION ?? ''),
        value: element,
      ));
    });
    return mList;
  }

  List<TableRow> getDefectList(BuildContext context) {
    List<TableRow> tempView = [];
    tempView.add(TableRow(
        children: [
          //增加行高
          SizedBox(
            height: 40,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                '缺陷代码',
                style: TextStyle(
                  color: theme_blue.toColor(),
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '缺陷描述',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '数量',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
        ],
        //第一行样式 添加背景色
        decoration: BoxDecoration(
          color: table_head.toColor(),
        )));
    for (int i = 0; i < state.defectList.length; i++) {
      LotSCRAPCODELIST element = state.defectList[i];
      tempView.add(TableRow(children: [
        //增加行高
        SizedBox(
          height: 40,
          child: Align(
            alignment: Alignment.center,
            child: Text(
              element.cODE ?? '',
              style: TextStyle(
                color: text_black.toColor(),
              ),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.dESCRIPTION ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.topCenter,
          child: Container(
            padding: EdgeInsets.only(bottom: 4),
            child: ClearEditText(
              "",
                  (value) {
                if (value.isEmpty) {
                  value = "0";
                }
                element.DEFECTQTY = value;
                // controller.updateDefectCount();
              },
              type: TextInputType.number,
              isScan: false,
              isShowIcon: false,
              controller: TextEditingController(text: element.DEFECTQTY),
            ),
          ),
        )
      ]));
    }
    return tempView;
  }

// getCheckList(BuildContext context) {
//   List<TableRow> tempView = [];
//   List<FocusNode> focusNodeList = [];
//   tempView.add(TableRow(
//       children: [
//         //增加行高
//         SizedBox(
//           height: 40,
//           child: Align(
//             alignment: Alignment.center,
//             child: Text(
//               '不良代码',
//               style: TextStyle(
//                 color: theme_blue.toColor(),
//               ),
//             ),
//           ),
//         ),
//         Align(
//           alignment: Alignment.center,
//           child: Text(
//             '不良描述',
//             style: TextStyle(
//               color: theme_blue.toColor(),
//             ),
//           ),
//         ),
//         Align(
//           alignment: Alignment.center,
//           child: Text(
//             '数量',
//             style: TextStyle(
//               color: theme_blue.toColor(),
//             ),
//           ),
//         )
//       ],
//       //第一行样式 添加背景色
//       decoration: BoxDecoration(
//         color: table_head.toColor(),
//       )));
//   for (int i = 0; i < state.bomList.length; i++) {
//     BinBean element = state.bomList[i];
//     focusNodeList.add(FocusNode());
//     tempView.add(TableRow(children: [
//       //增加行高
//       SizedBox(
//         height: 40,
//         child: Align(
//           alignment: Alignment.center,
//           child: Text(
//           element.key1,
//           style: TextStyle(
//             color: text_black.toColor(),
//           ),
//         ),
//         ),
//       ),
//       Align(
//         alignment: Alignment.center,
//         child: Text(
//           element.key2,
//           style: TextStyle(
//             color: text_black.toColor(),
//           ),
//         ),
//       ),
//       Align(
//         alignment: Alignment.center,
//         child: Text(
//           element.key3,
//           style: TextStyle(
//             color: text_black.toColor(),
//           ),
//         ),
//       )
//     ]));
//   }
//   return tempView;
// }
}
